<?php

include_once '../inc/db_util.php';

function findThread($keyword) {
    $array_announce = array();
    $jsArray = json_decode($keyword, false);
    $query = mysql_query("SELECT * FROM tb_thread tr
             JOIN tb_det_matkul dm ON dm.id_det_matkul = tr.id_det_matkul
             JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul            
             JOIN (
                SELECT nip id, namadosen nama, username FROM tb_dosen
                UNION                            
                SELECT id_mahasiswa id, nama, username FROM tb_mahasiswa
             ) user ON tr.id_user_create = user.id
             WHERE judul LIKE '%" . $jsArray->{'Judul'} . "%' OR isi LIKE '%" . $jsArray->{'Isi'} . "%'");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_thread'];
        $row_array['Judul'] = $row['judul'];
        $row_array['Isi'] = $row['isi'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['TahunAjaran'] = $row['tahun_ajaran'];
        $row_array['DetMatkul']['Creator']['Nip'] = $row['nip'];
        $row_array['DetMatkul']['Creator']['Nama'] = $row['namadosen'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];
        $row_array['Creator']['Id'] = $row['id'];
        $row_array['Creator']['Nama'] = $row['nama'];
        $row_array['Creator']['Username'] = $row['username'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function viewThread($filter) {
    $array_thread = array();
    if (!is_null($filter)) {
        $jsArray = json_decode($filter, false);
        $query = mysql_query("SELECT * FROM tb_thread tr
             JOIN tb_det_matkul dm ON dm.id_det_matkul = tr.id_det_matkul
             JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
             JOIN (
                SELECT nip id, namadosen nama, username FROM tb_dosen
                UNION                            
                SELECT id_mahasiswa id, nama, username FROM tb_dosen
             ) user ON tr.id_user_create = user.id
             WHERE tr.id_det_matkul='" . $jsArray->{'DetMatkul'}->{'Id'} . "'
             ");
    } else {
        $query = mysql_query("SELECT * FROM tb_thread tr
             JOIN tb_det_matkul dm ON dm.id_det_matkul = tr.id_det_matkul
             JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
             JOIN (
                SELECT nip id, namadosen nama, username FROM tb_dosen
                UNION                            
                SELECT id_mahasiswa id, nama, username FROM tb_mahasiswa
             ) user ON tr.id_user_create = user.id
             ");
    }

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_thread'];
        $row_array['Judul'] = $row['judul'];
        $row_array['Isi'] = $row['isi'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['TahunAjaran'] = $row['tahun_ajaran'];
        $row_array['DetMatkul']['Creator']['Nip'] = $row['nip'];
        $row_array['DetMatkul']['Creator']['Nama'] = $row['namadosen'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];
        $row_array['Creator']['Id'] = $row['id'];
        $row_array['Creator']['Nama'] = $row['nama'];
        $row_array['Creator']['Username'] = $row['username'];

        array_push($array_thread, $row_array);
    }
    return json_encode($array_thread);
}

function createThread($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_thread(id_user_create, username, id_det_matkul, judul, isi, tanggal_create)
        VALUES (            
        '" . $jsArray->{'Creator'}->{'Id'} . "',
        '" . $jsArray->{'Creator'}->{'Username'} . "',
        '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Judul'} . "',
        '" . $jsArray->{'Isi'} . "',
        NOW())";

    return mysql_query($sql);
}

function updateThread($json) {
    $jsArray = json_decode($json, false);
    $sql = "UPDATE tb_thread SET
        id_user_create = '" . $jsArray->{'Creator'}->{'Id'} . "',
        username = '" . $jsArray->{'Creator'}->{'Username'} . "',
        id_det_matkul = '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        judul = '" . $jsArray->{'Judul'} . "',
        isi = '" . $jsArray->{'Isi'} . "'
        WHERE id_thread = '" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function deleteThread($json) {
    $jsArray = json_decode($json, false);
    $sql = "DELETE FROM tb_posting WHERE id_thread = '" . $jsArray->{'Id'} . "'";

    if (mysql_query($sql))
        $sql = "DELETE FROM tb_thread WHERE id_thread = '" . $jsArray->{'Id'} . "'";
    return mysql_query($sql);
}

?>
